{% extends 'base/base.html' %}

{% block title %}{{ title }} - {{ project.name }} - 多用户报警系统{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h2 class="h4">
                    {% if member_to_update %}
                        <i class="fa fa-pencil" aria-hidden="true"></i> {{ title }}
                    {% else %}
                        <i class="fa fa-user-plus" aria-hidden="true"></i> {{ title }}
                    {% endif %}
                </h2>
            </div>
            <div class="card-body">
                <form method="post">
                    {% csrf_token %}
                    {% if form.errors %}
                        <div class="alert alert-danger" role="alert">
                            <i class="fa fa-exclamation-circle" aria-hidden="true"></i> 请检查以下错误并重新提交表单。
                        </div>
                    {% endif %}
                    <div class="mb-3">
                        <label for="id_user" class="form-label">
                            <i class="fa fa-user-o" aria-hidden="true"></i> 用户 <span class="text-danger">*</span>
                        </label>
                        {{ form.user }}
                        {% if form.user.errors %}
                            <div class="text-danger mt-1">
                                {% for error in form.user.errors %}
                                    <i class="fa fa-exclamation-triangle" aria-hidden="true"></i> {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>
                    <div class="mb-4">
                        <label for="id_role" class="form-label">
                            <i class="fa fa-key" aria-hidden="true"></i> 角色 <span class="text-danger">*</span>
                        </label>
                        {{ form.role }}
                        {% if form.role.errors %}
                            <div class="text-danger mt-1">
                                {% for error in form.role.errors %}
                                    <i class="fa fa-exclamation-triangle" aria-hidden="true"></i> {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text text-muted">
                            <small><i class="fa fa-info-circle" aria-hidden="true"></i> 管理员可以管理项目和成员，程序员可以编辑项目内容，运营只能查看项目内容。</small>
                        </div>
                    </div>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-check" aria-hidden="true"></i> 保存
                        </button>
                        <a href="{% url 'projects:project_members' project.id %}" class="btn btn-secondary">
                            <i class="fa fa-times" aria-hidden="true"></i> 取消
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block javascript %}
<script>
    // 添加页面加载动画效果
    document.addEventListener('DOMContentLoaded', function() {
        // 为表单元素添加焦点效果
        const formInputs = document.querySelectorAll('.form-control');
        formInputs.forEach(input => {
            input.addEventListener('focus', function() {
                this.parentElement.classList.add('shadow-sm');
            });
            input.addEventListener('blur', function() {
                this.parentElement.classList.remove('shadow-sm');
            });
        });
        
        // 为用户选择框添加搜索功能
        const userSelect = document.getElementById('id_user');
        if (userSelect && !userSelect.disabled) {
            // 这里可以添加自定义的搜索功能，或者使用select2等库
            console.log('可以为用户选择框添加搜索功能');
        }
    });
</script>
{% endblock %}